<template>
    <div class="login-wrap">
        <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"
            class="demo-ruleForm login-container">
            <h3 class="title">用户注册</h3>
            <el-form-item prop="Name">
                <el-input type="text" v-model="ruleForm.Name" auto-complete="off" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item prop="Password">
                <el-input type="Password" v-model="ruleForm.Password" auto-complete="off" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item prop="Phone">
                <el-input type="text" v-model="ruleForm.Phone" auto-complete="off" placeholder="手机号"></el-input>
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item prop="Code">
                        <el-input type="text" v-model="ruleForm.Code" auto-complete="off" placeholder="图形验证码"
                            @keyup.enter.native="submitForm('ruleForm')"></el-input>
                    </el-form-item>
                </el-col>
                <div @click="refreshCode()" class="Code" style="cursor:pointer;" title="点击切换验证码">
                    <s-identify :identifyCode="identifyCode"></s-identify>
                </div>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <span class="remember" @click="goLogin">去登录</span>
                </el-col>
            </el-row>

            <el-form-item style="width:100%;">
                <el-button type="primary" style="width:100%;" @click="submitForm('ruleForm')"
                    :loading="logining">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script type="text/ecmascript-6">
import sIdentify from "@/components/sIdentify.vue";
export default {
    components: { sIdentify },
    name: 'login',
    data() {
        return {
            //定义loading默认为false
            logining: false,
            ruleForm: {
                //username和password默认为空
                Name: '',
                Password: '',
                Phone: "",
                Code: '',
                randomStr: '',
                codeimg: ''
            },
            identifyCode: "",
            identifyCodes: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'],
            //rules前端验证
            rules: {
                Name: [{ required: true, message: '请输入账号', trigger: 'blur' }, { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }],
                Password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }],
                Phone: [{ required: true, message: '请输入手机号', trigger: 'blur' },
                { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
                Code: [{ required: true, message: '请输入验证码', trigger: 'blur' },
                { validator: this.validateCode, trigger: 'blur' }]
            }
        }
    },
    // 创建完毕状态(里面是操作)
    created() {
        // getLogin()
        // 在页面加载时重置密码
        console.log(this.ruleForm.Password, 'this.ruleForm.password111')
        this.ruleForm.Password = '';
        console.log(this.ruleForm.Password, 'this.ruleForm.password222')
    },
    mounted() {
        this.refreshCode()
    },
    unmounted() {

    },
    // 里面的函数只有调用才会执行
    methods: {
        goLogin() {
            this.$router.push('/login')
        },
        validateCode(rule, value, callback) {
            if (value === this.identifyCode) {
                callback(); // 校验通过
            } else {
                callback(new Error('验证码不正确')); // 校验失败，返回错误信息
            }
        },
        // 生成随机数
        randomNum(min, max) {
            max = max + 1
            return Math.floor(Math.random() * (max - min) + min);
        },
        // 更新验证码
        refreshCode() {
            this.identifyCode = "";
            this.makeCode(this.identifyCodes, 4);
            console.log('当前验证码:', this.identifyCode);
        },
        // 随机生成验证码字符串
        makeCode(data, len) {
            console.log('data, len:', data, len)
            for (let i = 0; i < len; i++) {
                this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length - 1)]
            }
        },
        //获取info列表
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.logining = true
                    // 测试通道，不为空直接登录
                    setTimeout(() => {
                        this.logining = false
                        this.$store.commit('login', 'true')
                        this.$router.push({ path: '/goods/Goods' })
                    }, 1000)
                } else {
                    // 获取图形验证码
                    this.getcode()
                    this.$message.error('请输入用户名密码！')
                    this.logining = false
                    return false
                }
            })
        },
    }
}
</script>

<style scoped>
.login-wrap {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    padding-top: 10%;
    background-image: url();
    /* background-color: #112346; */
    background-repeat: no-repeat;
    background-position: center right;
    background-size: 100%;
}

.login-container {
    border-radius: 10px;
    margin: 0px auto;
    width: 350px;
    padding: 30px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    text-align: left;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
}

.remember {
    float: left;
    margin: 0px 0px 35px 0px;
    cursor: pointer;
}

.Code-box {
    text-align: right;
}

.codeimg {
    height: 40px;
}
</style>